﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Business.Compta;

namespace Manageo
{
    /// <summary>
    /// Interaction logic for MainForm.xaml
    /// </summary>
    public partial class MainForm : UserControl
    {
        private User connectedUser;

        public delegate void myBeginDelegate();
        myBeginDelegate beginDel;

        public delegate void myChangedDelegate(NotifyEventArgs i);
        myChangedDelegate changedDel;

        public delegate void myEndedDelegate();
        myEndedDelegate endedDel;

        public MainForm()
        {
            InitializeComponent();
        }


        public MainForm(User _connectedUser)
        {
            InitializeComponent();

            beginDel = new myBeginDelegate(taskBegin);
            changedDel = new myChangedDelegate(taskChanged);
            endedDel = new myEndedDelegate(taskEnded);
            this.connectedUser = _connectedUser;
            if ("cdagorne".Equals(connectedUser.Login))
            {
                addCdcGroupMenu();
                addCertifGroupMenu();
            }
        }

        private void addCdcGroupMenu()
        {
            DevExpress.Wpf.NavBar.NavBarGroup newNavBarGroup = new DevExpress.Wpf.NavBar.NavBarGroup();
            newNavBarGroup.Header = "Cdc";
            DevExpress.Wpf.NavBar.NavBarItem Import = new DevExpress.Wpf.NavBar.NavBarItem();
            Import.Name = "ImportBarItem";
            Import.Content = "Import";
            DevExpress.Wpf.NavBar.NavBarItem Generate = new DevExpress.Wpf.NavBar.NavBarItem();
            Generate.Content = "Generate";
            Generate.Name = "GenerateBarItem";
            DevExpress.Wpf.NavBar.NavBarItem View = new DevExpress.Wpf.NavBar.NavBarItem();
            View.Content = "View";
            View.Name = "ViewBarItem";
            newNavBarGroup.Items.Add(Import);
            newNavBarGroup.Items.Add(Generate);
            newNavBarGroup.Items.Add(View);
            this.Menu.Groups.Add(newNavBarGroup);
        }

        private void addCertifGroupMenu()
        {
            DevExpress.Wpf.NavBar.NavBarGroup newNavBarGroup = new DevExpress.Wpf.NavBar.NavBarGroup();
            newNavBarGroup.Header = "Certif";
            DevExpress.Wpf.NavBar.NavBarItem Chapter1 = new DevExpress.Wpf.NavBar.NavBarItem();
            Chapter1.Name = "Chapter1";
            Chapter1.Content = "Chapter1";
            DevExpress.Wpf.NavBar.NavBarItem Chapter6 = new DevExpress.Wpf.NavBar.NavBarItem();
            Chapter6.Content = "Chapter6";
            Chapter6.Name = "Chapter6";
            DevExpress.Wpf.NavBar.NavBarItem Chapter7 = new DevExpress.Wpf.NavBar.NavBarItem();
            Chapter7.Content = "Chapter7";
            Chapter7.Name = "Chapter7";
            DevExpress.Wpf.NavBar.NavBarItem animation = new DevExpress.Wpf.NavBar.NavBarItem();
            animation.Content = "Animation";
            animation.Name = "Animation";
            newNavBarGroup.Items.Add(Chapter1);
            newNavBarGroup.Items.Add(Chapter6);
            newNavBarGroup.Items.Add(Chapter7);
            newNavBarGroup.Items.Add(animation);
            this.Menu.Groups.Add(newNavBarGroup);
        }

        private void selectMenu(object sender, DevExpress.Wpf.NavBar.NavBarItemSelectedEventArgs e)
        {
            if ("ImportBarItem".Equals(e.Item.Name))
            {
                Cdc.Import importView = new Cdc.Import();
                importView.Changed += new INotifierUI.ChangedEventHandler(advancementChanged);
                importView.Begin += new INotifierUI.ChangedEventHandler(taskBegin);
                importView.Ended += new INotifierUI.ChangedEventHandler(taskEnded);
                this.MainPanel.Content = importView;
            }
            if ("GenerateBarItem".Equals(e.Item.Name))
            {
                this.MainPanel.Content = new Cdc.Generation();
            }
            if ("ViewBarItem".Equals(e.Item.Name))
            {
                Cdc.View view = new Cdc.View();
                view.Changed += new INotifierUI.ChangedEventHandler(advancementChanged);
                view.Begin += new INotifierUI.ChangedEventHandler(taskBegin);
                view.Ended += new INotifierUI.ChangedEventHandler(taskEnded);
                this.MainPanel.Content = view;
            }
            if ("ComptaLineItem".Equals(e.Item.Name))
            {
                Compta.ComptaLineUI cpLineUI = new Compta.ComptaLineUI(connectedUser.Id);
                cpLineUI.Changed += new INotifierUI.ChangedEventHandler(advancementChanged);
                cpLineUI.Begin += new INotifierUI.ChangedEventHandler(taskBegin);
                cpLineUI.Ended += new INotifierUI.ChangedEventHandler(taskEnded);
                cpLineUI.IsVisibleChanged += new DependencyPropertyChangedEventHandler(cpLineUI_IsVisibleChanged);
                this.MainPanel.Content = cpLineUI;
            }
            if ("Categories".Equals(e.Item.Name))
            {
                this.MainPanel.Content = new Compta.CategoriesDefinition();
            }
            if ("Users".Equals(e.Item.Name))
            {
                this.MainPanel.Content = new Compta.UsersUI();
            }
            if ("Accounts".Equals(e.Item.Name))
            {
                this.MainPanel.Content = new Compta.AccountsUI();
            }
            if ("Statements".Equals(e.Item.Name))
            {
                this.MainPanel.Content = new Compta.StatementsUI(connectedUser.Id);
            }
            if ("Chapter1".Equals(e.Item.Name))
            {
                this.MainPanel.Content = new Certification.Chapter1();
            }
            if ("Chapter6".Equals(e.Item.Name))
            {
                this.MainPanel.Content = new Certification.Chapter6_DataBinding();
            }
            if ("Chapter7".Equals(e.Item.Name))
            {
                this.MainPanel.Content = new Certification.Chapter7_DataTemplates();
            }
            if ("Animation".Equals(e.Item.Name))
            {
                this.MainPanel.Content = new Certification.Animation();
            }
            if ("Synthese".Equals(e.Item.Name))
            {
                this.MainPanel.Content = new Compta.SyntheseUser(connectedUser.Id);
            }
            if ("Consommations".Equals(e.Item.Name))
            {
                this.MainPanel.Content = new Compta.ComptaConsommationsUI();
            }


        }

        void cpLineUI_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            Compta.ComptaLineUI cptaLine = (Compta.ComptaLineUI)sender;
            if ((bool)e.NewValue == false)
            {
                try
                {
                    cptaLine.saveLayout();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Problème lors de la sauvegarde de la disposition des colonnes : \n" + ex.Message + "\n\n" + ex.StackTrace, "ERROR !!!", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else
            {
                try
                {
                    cptaLine.loadLayout();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Problème lors de la récupération de la disposition des colonnes : \n" + ex.Message + "\n\n" + ex.StackTrace, "ERROR !!!", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

        void taskEnded(object sender, NotifyEventArgs e)
        {
            System.Windows.Threading.Dispatcher aDisp = progressPb.Dispatcher;
            aDisp.Invoke(endedDel, System.Windows.Threading.DispatcherPriority.Normal);
        }

        void taskBegin(object sender, NotifyEventArgs e)
        {
            System.Windows.Threading.Dispatcher aDisp = progressPb.Dispatcher;
            aDisp.Invoke(beginDel, System.Windows.Threading.DispatcherPriority.Normal);
        }

        private void taskBegin()
        {
            this.progressPb.Value = 0;
            this.progressPb.Visibility = Visibility.Visible;
        }

        private void taskEnded()
        {
            this.progressPb.Visibility = Visibility.Hidden;
            this.infoStatusBarTb.Visibility = Visibility.Hidden;
        }

        private void taskChanged(NotifyEventArgs arg)
        {
            this.progressPb.Value = arg.Advancement;
            if (String.IsNullOrEmpty(arg.Message))
            {
                this.infoStatusBarTb.Visibility = Visibility.Hidden;
            }
            else
            {
                this.infoStatusBarTb.Visibility = Visibility.Visible;
                this.infoStatusBarTb.Text = arg.Message;
            }
        }
        void advancementChanged(object sender, NotifyEventArgs e)
        {
            System.Windows.Threading.Dispatcher aDisp = progressPb.Dispatcher;
            aDisp.Invoke(changedDel, System.Windows.Threading.DispatcherPriority.Normal, e);
        }
    }

    public class RedToGreenScaleConverter : System.Windows.Markup.MarkupExtension, IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int g = (int)((double)value * 2.55);
            int r = 255 - g;            
            return new SolidColorBrush(Color.FromArgb(255, (byte)r, (byte)g, 0));
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
    }


}
